import java.util.concurrent.atomic.AtomicInteger;

public class demo1 {
//    public static int count = 0;
    //使用这个类进行自增是“原子性”，底层是计算机硬件的CAS指令1
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger(0);
        Thread t1 = new Thread(() ->{
            for(int i = 0;i<5000;i++){
                atomicInteger.getAndIncrement();
            }
        });
        Thread t2 = new Thread(() ->{
            for(int i = 0;i<5000;i++){
                atomicInteger.getAndIncrement();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(atomicInteger.get());
    }
}
